import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-10-27
 * Time: 19:09
 */
public class test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int flag = in.nextInt();
            while (flag != 0) {
                int n = in.nextInt();
                int k = in.nextInt();
                char[] comp = in.next().toCharArray();
                int t = 0;
                int num = 0;
                for (char ch : comp) {
                    if (ch == 'W') {
                        t++;
                        if (t >= 3) {
                            num += k;
                        } else {
                            num++;
                        }
                    } else {
                        num--;
                        t = 0;
                    }
                }
                System.out.println(num);
                flag--;
            }
        }
    }
}
